const express = require('express')
const puppeteer = require('puppeteer');
const {Worker,markAsUntransferable} = require('worker_threads');

let lottery_data,stock_data
(async ()=>{
    let lottery = new Worker('./lottery.js')
    lottery.on('message', o => {
        lottery_data=o
        console.log(o)
    })
    lottery.on('error',o => console.log('lottery',o))

    let stock = new Worker('./stock.js')
    stock.on('message', o => {
        stock_data = o
        console.log(o)
    })
    stock.on('error', o => console.log('stock',o))
})()

const app = express()
app.use(express.static(__dirname+'/pages/dist'))
app.get('/',function(req,res){
    res.sendFile(__dirname+'/pages/dist/index.html')
})
app.get('/list',function(req,res){
    stock.postMessage(req.params['marked'])
})
app.get('/stock',function(req,res){
    res.send(stock_data.Result.fivedays)
})
app.get('/lottery',function(req,res){
    res.send(lottery_data)
})
app.listen(80,()=>{
    process.title='stock scan server';
    console.log('id:',process.pid)
})